package com.zyz.I_多线程.创建方式.Callable;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author: vick
 * @Date: 2025/3/21
 * @Description:
 * @version: 1.0
 */
public class Test {
    public static void main(String[] args) {
        //1. 创建Callable接口的实现类 对象
        TestCallable testCallable = new TestCallable();

        //2. 将Callable接口的实现类 对象 作为参数传递FutureTask构造器中，创建FutureTask对象
        FutureTask<Integer> futureTask = new FutureTask(testCallable);

        //3. 将FutureTask的对象传递到Thread类的构造器中，创建Thread对象
        Thread thread = new Thread(futureTask);

        //4.启动线程
        thread.start();

        //接收返回值
        //5. 获取Callable 中的call方法的返回值
        try {
            Integer sum = futureTask.get();
            System.out.println("总和是：" + sum);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
    }
}
